package edu.nwmissouri.galaxygames;

import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class LifeCounterFragment extends Fragment {

	Activity activity;
		
	//---The ArrayAdapter that allows us to populate the ListViews with an array---
	CustomLifeAdapter lifeAdapt;
	
	//Declare onScrollListener
	OnScrollListener snapper;
	
	//Caching values
	public static int lifeTotalOne;
	public static int lifeTotalTwo;
	
	//---The ListViews that will display the players' life totals---
	static LifeListView playerOneLifeLV;
	static LifeListView playerTwoLifeLV;
	//---The buttons that will reset the player life total back to 20
	Button playerOneResetBTN;
	Button playerTwoResetBTN;
	
	//For calculating button size dynamically
	static LinearLayout ll;
	
	//---The standardized text size for all buttons in the Life Counter
	float btnTextSize;
	
	//---The DisplayMetrics and corresponding variables allow us to 
	//---change the size of the ListViews dependent on the device---
	DisplayMetrics metrics;
	int width;
	int height;
	//---Variables to hold the permanent values of width and height for the list views
	int lvHeight;
	int lvWidth;
	int lvHeightCenter;
	
	int lowHeight;
	int titleBarHeight;
	
	//---Variables that will hold the value necessary to center the selected View in the ListView
	static int selectionCenter;
	
	//---int constant that holds the size of the lifeTotals array
	final int LIFE_TOTAL_SIZE = 1000;
	//---An array of 100 elements that will keep track of life totals up to 999---
	String [] lifeTotals = new String [LIFE_TOTAL_SIZE];
	
	//---Default constructors
	public LifeCounterFragment(){
		this.activity = getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle bundle)
	{
	    return inflater.inflate(R.layout.activity_life_counter, view, false);
	}
	
	public void onCreate(Bundle bundle)
	{
	    super.onCreate(bundle);
	}
		
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		//---metrics holds the DisplayMetrics whenever the app is launched
		//---this way, we will have correct values no matter what the screen size is
		metrics = getResources().getDisplayMetrics();
		//---Store the height in pixels of the current device
		height = metrics.heightPixels;
		//---Store the width in pixels of the current device
		width = metrics.widthPixels;
		//---These adjusted values represent the height and width of our ListViews
		//---Which is 1/4 the size of the screen each
		lvHeight = height/2;
		lvWidth = width/2;
		lvHeightCenter = lvHeight/2;
		
        //---Initializing the ListViews
		playerOneLifeLV = (LifeListView) getActivity().findViewById(R.id.playerOneLifeLV);
		playerTwoLifeLV = (LifeListView) getActivity().findViewById(R.id.playerTwoLifeLV);
		
		//--Set the layout parameters for the ListView so its height and width are both
		//--half the size of the screen, making it take up 1/4 of the screen
		playerOneLifeLV.setLayoutParams(new LinearLayout.LayoutParams(lvWidth, lvHeight));				
		playerTwoLifeLV.setLayoutParams(new LinearLayout.LayoutParams(lvWidth, lvHeight));
		
		
		//---For loop that fills the array "lifeTotals" will values from 0-999
		for(int i = 0; i < lifeTotals.length; i++){
			String life = i + "";
			lifeTotals[i] = life;
		}
		
		
		//---Initializing our custom ArrayAdapter that will allow us to customize the Views inside the ListView
		lifeAdapt = new CustomLifeAdapter(getActivity().getBaseContext(), lifeTotals, lvHeight, lvWidth);
		//--Populate the PlayerOneLife ListView
		playerOneLifeLV.setAdapter(lifeAdapt);
		playerOneLifeLV.setFocusableInTouchMode(true);
		//---Populate the PlayerTwoLife ListView
		playerTwoLifeLV.setAdapter(lifeAdapt);
		playerTwoLifeLV.setFocusableInTouchMode(true);
		
		//Create and register our onScrollListener.
		snapper = new SnapAfterFling(lvHeight);
		playerOneLifeLV.setOnScrollListener(snapper);
		playerTwoLifeLV.setOnScrollListener(snapper);
		
		//Center the listviews on 20 life.
		selectionCenter =(int) (lvHeight * .25);
		playerOneLifeLV.setSelectionFromTop(20, selectionCenter);
		playerTwoLifeLV.setSelectionFromTop(20, selectionCenter);

		

	}
	@Override
	public void onStart()
	{
		super.onStart();

		//Get reset buttons so we can dynamically setting their font size.
		playerOneResetBTN = (Button)this.getActivity().findViewById(R.id.playerOneResetBTN);
		playerTwoResetBTN = (Button)this.getActivity().findViewById(R.id.playerTwoResetBTN);
		
		//Checking orientation
		if(width > height){
			btnTextSize = (float)(height * .1);
		}else
		{
			btnTextSize = (float)(width * .05);
		}
		
		playerOneResetBTN.setTextSize(btnTextSize);
		
		
		//Get status bar height
		Rect rectgle= new Rect();
		Window window= getActivity().getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
		int StatusBarHeight= rectgle.top;
		int contentViewTop= 
		window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
		titleBarHeight = contentViewTop - StatusBarHeight;
		
		//Initialize lower view and set button size accordingly
		ll = (LinearLayout) this.getActivity().findViewById(R.id.lifeCounterLowerLayout);
		lowHeight = (int) ((int) height - lvHeight - titleBarHeight - (titleBarHeight - playerOneLifeLV.getY()));
		width = metrics.widthPixels;
		int buttonHeight = lowHeight - (lowHeight/6);
		int buttonWidth = (width/2) - (width/10);
		int topMargin = (int) ((lowHeight * 0.16));
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(buttonWidth, buttonHeight);
		params.setMargins(width/20, topMargin, width/20, topMargin);
		playerOneResetBTN.setLayoutParams(params);
		params.setMargins(width/20, topMargin, width/20, topMargin);
		playerTwoResetBTN.setLayoutParams(params);
		
		

		playerTwoResetBTN.setTextSize(btnTextSize);	

	}
	

	@Override
	public void onConfigurationChanged(Configuration config){
		super.onConfigurationChanged(config);
		//---metrics holds the DisplayMetrics whenever the app is launched
		//---this way, we will have correct values no matter what the screen size is
		metrics = getResources().getDisplayMetrics();
		//---Store the height in pixels of the current device
		height = metrics.heightPixels;
		//---Store the width in pixels of the current device
		width = metrics.widthPixels;
		//---These adjusted values represent the height and width of our ListViews
		//---Which is 1/4 the size of the screen each
		lvHeight = height/2;
		lvWidth = width/2;
		lvHeightCenter = lvHeight/2;
				
		//---Initializing the ListViews
		playerOneLifeLV = (LifeListView) getActivity().findViewById(R.id.playerOneLifeLV);
		playerTwoLifeLV = (LifeListView) getActivity().findViewById(R.id.playerTwoLifeLV);
		
		//--Set the layout parameters for the ListView so its height and width are both
		//--half the size of the screen, making it take up 1/4 of the screen
		playerOneLifeLV.setLayoutParams(new LinearLayout.LayoutParams(lvWidth, lvHeight));				
		playerTwoLifeLV.setLayoutParams(new LinearLayout.LayoutParams(lvWidth, lvHeight));
		
		//Get status bar height
				Rect rectgle= new Rect();
				Window window= getActivity().getWindow();
				window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
				int StatusBarHeight= rectgle.top;
				int contentViewTop= 
				window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
				titleBarHeight = contentViewTop - StatusBarHeight;
		
		//Set button sizes
		playerOneResetBTN = (Button) this.getActivity().findViewById(R.id.playerOneResetBTN);
		playerTwoResetBTN = (Button) this.getActivity().findViewById(R.id.playerTwoResetBTN);
		ll = (LinearLayout) this.getActivity().findViewById(R.id.lifeCounterLowerLayout);
		lowHeight = (int) ((int) height - lvHeight - titleBarHeight - (titleBarHeight - playerOneLifeLV.getY()));
		width = metrics.widthPixels;
		int buttonHeight = lowHeight - (lowHeight/6);
		int buttonWidth = (width/2) - (width/10);
		int topMargin = (int) ((lowHeight * 0.16));
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(buttonWidth, buttonHeight);
		params.setMargins(width/20, topMargin, width/20, topMargin);
		playerOneResetBTN.setLayoutParams(params);
		params.setMargins(width/20, topMargin, width/20, topMargin);
		playerTwoResetBTN.setLayoutParams(params);
		
		
	}

}
